package com.bookbuf.module.stub;

import android.text.TextUtils;

import com.bookbuf.module.Proxy;
import com.bookbuf.module.ProxyManager;

/**
 * author: robert.
 * date :  2016/12/26.
 */

public abstract class Stub<UI, Service, Dependence> extends Proxy<UI, Service, Dependence> {

    private Proxy<UI, Service, Dependence> proxy;

    // XXX: 2016/12/29 由stub向proxy对象注入dependence
    @Override
    public void injectDependence(Dependence dependence) {
        super.injectDependence(dependence);
        getProxy().injectDependence(getDependence());
    }

    @Override
    public final UI uiInterface() {
        return getProxy().uiInterface();
    }

    @Override
    public final Service serviceInterface() {
        return getProxy().serviceInterface();
    }

    @Override
    public final Dependence dependenceInterface() {
        return getProxy().dependenceInterface();
    }

    protected abstract String getModuleClassName();

    private Proxy<UI, Service, Dependence> getProxy() {
        if (proxy == null) {
            String module = getModuleClassName();
            if (!TextUtils.isEmpty(module)) {
                proxy = (Proxy<UI, Service, Dependence>) ProxyManager.newInstance(module);
            }
        }
        return proxy;
    }
}
